//
//  NumberCountView.swift
//  WangPu
//
//  Created by xiaozhi on 15/9/25.
//  Copyright (c) 2015年 zhixiao. All rights reserved.
//

import UIKit

class NumberCountView: UIView {
    var addButton:UIButton!
    var numberLabel:UILabel!
    var reduceButton:UIButton!
    var minNumber:Int = 0
    var numberChangedHandle:((NSInteger)->Void)?
    var currentNumber:NSInteger = 0{
        didSet{
            numberLabel.text = "\(currentNumber)"
        }
    }
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    func createSubViews()->Void{
        self.clipsToBounds = true
        layer.borderColor = lightBackgoundColor.CGColor
        layer.borderWidth = onePix
        layer.cornerRadius = bounds.size.height/2.0
        
        addButton = UIButton(frame: CGRectMake(0,0, bounds.size.width/3.0, bounds.size.height))
        addButton.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        addButton.setTitle("+", forState: UIControlState.Normal)
        addButton.titleLabel?.font = UIFont.systemFontOfSize(20)
        addButton.addTarget(self, action: "addBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
        self.addSubview(addButton)
        
        numberLabel = UILabel(frame: CGRectMake(bounds.size.width/3.0, 0, bounds.size.width/3.0, bounds.size.height))
        numberLabel.textColor = UIColor.darkGrayColor()
        numberLabel.font = UIFont.boldSystemFontOfSize(13)
        numberLabel.text = "0"
        numberLabel.textAlignment = NSTextAlignment.Center
        numberLabel.clipsToBounds = true
        numberLabel.layer.borderWidth = onePix
        numberLabel.layer.borderColor = lightBackgoundColor.CGColor
        self.addSubview(numberLabel)
        
        reduceButton = UIButton(frame: CGRectMake( bounds.size.width*2/3.0,0, bounds.size.width/3.0, bounds.size.height))
        reduceButton.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        reduceButton.setTitle("-", forState: UIControlState.Normal)
        reduceButton.titleLabel?.font = UIFont.systemFontOfSize(24)
        reduceButton.addTarget(self, action: "reduceBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
        self.addSubview(reduceButton)
        self.hideButton(true)
    }
    func hideButton(hidden:Bool)->Void{
        addButton.hidden = hidden
        reduceButton.hidden = hidden
        if hidden{
            layer.borderColor = UIColor.clearColor().CGColor
        }
        else{
            layer.borderColor = lightBackgoundColor.CGColor
        }
    }
    @IBAction func addBtnClick(button:UIButton)->(){
        currentNumber+=1;
        reduceButton.enabled = true
        numberChangedHandle?(currentNumber)
    }
    @IBAction func reduceBtnClick(button:UIButton)->(){
        if currentNumber==minNumber{
            reduceButton.enabled = false
            return
        }
        currentNumber-=1
        numberChangedHandle?(currentNumber)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        //        self.backgroundColor = UIColor(patternImage: UIImage(named: "tab-bg")!)
        backgroundColor = UIColor.whiteColor()
        self.createSubViews()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.createSubViews()
    }
}
